
#include "timer.h"
uint16_t tick;
extern uint32_t		uptime;
extern void  Delay(unsigned long  ulVal);
TIM_TIMERCFG_Type	TIM_ConfigStruct;
TIM_MATCHCFG_Type	TIM_MatchConfigStruct;
//extern void send_status_trap(int value,unsigned char Arg);

void timer_init(void)
{
	TIM_DeInit(LPC_TIM0);
	TIM_ConfigStructInit(TIM_TIMER_MODE,&TIM_ConfigStruct);
	TIM_ConfigStruct.PrescaleOption	=	TIM_PRESCALE_USVAL;
	TIM_ConfigStruct.PrescaleValue	=	100;	
	// use channel 0, MR0
	TIM_MatchConfigStruct.MatchChannel = 0;	
	// Enable interrupt when MR0 matches the value in	TC register
	TIM_MatchConfigStruct.IntOnMatch	 = ENABLE;		
	//Enable reset on MR0: TIMER will	reset	if MR0 matches it
	TIM_MatchConfigStruct.ResetOnMatch = ENABLE;	
	//Stop on MR0 if MR0 matches it
	TIM_MatchConfigStruct.StopOnMatch	 = DISABLE;	
	//Toggle MR0.0 pin if	MR0	matches	it
	TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_NOTHING;	
	// Set Match value,	count	value	of 10000 (10000	*	100uS	=	1000000us	=	1s --> 1 Hz)
	TIM_MatchConfigStruct.MatchValue	 = 100;		
	// Set configuration for Tim_config	and	Tim_MatchConfig
	TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
	TIM_ConfigMatch(LPC_TIM0,&TIM_MatchConfigStruct);
	TIM_Cmd(LPC_TIM0,ENABLE);
}	
	
void TIMER0_IRQHandler(void)			
{	
	TIM_ClearIntPending(LPC_TIM0,TIM_MR0_INT);	
	uptime ++;
	tick++;
}
